package com.mfl.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.mfl.common.PageResult;
import com.mfl.constant.MessageConstant;
import com.mfl.context.BaseContext;
import com.mfl.exception.CourseCategoryAlreadyExistsException;
import com.mfl.mapper.CourseCategoryMapper;
import com.mfl.mapper.KYCourseVideoMapper;
import com.mfl.pojo.dto.CourseCategoryDTO;
import com.mfl.pojo.dto.CourseCategoryPageQueryDTO;
import com.mfl.pojo.entity.CourseCategory;
import com.mfl.pojo.vo.CourseCategoryPageQueryVO;
import com.mfl.pojo.vo.CourseCategoryVO;
import com.mfl.service.CourseCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.beans.Transient;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class CourseCategoryServiceImpl implements CourseCategoryService {

    @Autowired
    private CourseCategoryMapper courseCategoryMapper;
    @Autowired
    private KYCourseVideoMapper kyCourseVideoMapper;

    /**
     * 新增课程分类
     * @param courseCategoryDTO
     */
    @Override
    public void insert(CourseCategoryDTO courseCategoryDTO) {
        // 判断课程分类名称是否已存在
        CourseCategory courseCategory = courseCategoryMapper.getByCourseCategoryName(courseCategoryDTO.getCourseCategoryName());
        if(courseCategory != null){
            throw new CourseCategoryAlreadyExistsException(MessageConstant.COURSE_CATEGORY_ALREADY_EXISTS);
        }
        CourseCategory build = CourseCategory.builder()
                .courseCategoryName(courseCategoryDTO.getCourseCategoryName())
                .status(courseCategoryDTO.getStatus())
                .createUser(BaseContext.getCurrentId())
                .createTime(LocalDateTime.now())
                .build();
        courseCategoryMapper.insert(build);
    }

    /**
     * 分页查询课程分类
     * @param courseCategoryPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(CourseCategoryPageQueryDTO courseCategoryPageQueryDTO) {
        PageHelper.startPage(courseCategoryPageQueryDTO.getPage(), courseCategoryPageQueryDTO.getPageSize());
        Page<CourseCategoryPageQueryVO> page = courseCategoryMapper.pageQuery(courseCategoryPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除课程分类
     * @param ids
     */
    @Transactional
    @Override
    public void batchDelete(List<Integer> ids) {
        courseCategoryMapper.deleteByIds(ids);
        // 删除课程分类对应的课程视频
        kyCourseVideoMapper.deleteByCategoryId(ids);
    }

    /**
     * 修改课程分类状态
     * @param status
     * @param id
     */
    @Override
    public void updateStatus(Integer status, Integer id) {
        courseCategoryMapper.updateStatus(status, id);
    }

    /**
     * 根据课程分类id查询课程分类
     * @param id
     * @return
     */
    @Override
    public CourseCategoryVO getByCourseCategoryId(int id) {
        CourseCategory courseCategory = courseCategoryMapper.getByCourseCategoryId(id);
        CourseCategoryVO vo = CourseCategoryVO.builder()
                .id(courseCategory.getId())
                .courseCategoryName(courseCategory.getCourseCategoryName())
                .status(courseCategory.getStatus())
                .build();
        return vo;
    }

    /**
     * 修改课程分类
     * @param courseCategoryDTO
     */
    @Override
    public void update(CourseCategoryDTO courseCategoryDTO) {
        CourseCategory courseCategory = CourseCategory.builder()
                .id(courseCategoryDTO.getId())
                .courseCategoryName(courseCategoryDTO.getCourseCategoryName())
                .status(courseCategoryDTO.getStatus())
                .updateUser(BaseContext.getCurrentId())
                .updateTime(LocalDateTime.now())
                .build();
        courseCategoryMapper.update(courseCategory);
    }

    /**
     * 查询所有启用的课程分类
     * @return
     */
    @Override
    public List<CourseCategoryVO> getAll() {
        List<CourseCategoryVO> voList = courseCategoryMapper.selectAll();
        return voList;
    }
}
